// pages/sale/detail/detail.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 0, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '选择商品', //导航栏 中间的标题
    },
    height: app.globalData.height,
    isIphoneX: false, //是否是iphoneX
    price: '0.00', //预计退款
    pricees: '0.00', //预计退款
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          _this.setData({
            isIphoneX: true
          })
        }
      }
    })
    _this.setData({
      loadModal: true,
      beforeprice: options.price,
      before:options.before
    })

    $api.selectOrdersGoods({
      login_token: wx.getStorageSync('login_token'),
      order_id: options.id
    }).then((res) => {
      for (var i = 0; i < res.data.data.length; i++) {
        res.data.data[i].icon = false
      }
      _this.setData({
        id: options.id,
        list: res.data.data,
        fee: res.data.fee,
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })




  },
  //选择商品
  onChoice(e) {
    var _this = this;
    var id = e.currentTarget.dataset.id;
    for (var i = 0; i < _this.data.list.length; i++) {
      // icon 的选中
      if (_this.data.list[i].id == id) {
        var icon = !_this.data.list[i].icon
        _this.setData({
          ['list[' + i + '].icon']: icon
        })
        // 计算
        if (icon) {
          var price = (parseFloat(_this.data.price) + (_this.data.list[i].price * _this.data.list[i].num)).toFixed(2)
        } else {
          var price = (parseFloat(_this.data.price) - (_this.data.list[i].price * _this.data.list[i].num)).toFixed(2)
        }
        _this.setData({
          price: price
        })
        console.log(price)
      }
    }
    console.log(parseFloat(_this.data.beforeprice) > parseFloat(price))
    if (parseFloat(_this.data.beforeprice) > parseFloat(price)) {
      _this.setData({
        pricees: price
      })
    } else {
      _this.setData({
        pricees: _this.data.beforeprice
      })
    }

  },
  //确认提交
  onSubmit() {
    var _this = this;
    var goods = [];
    for (var i = 0; i < _this.data.list.length; i++) {
      if (_this.data.list[i].icon) {
        goods = goods.concat(_this.data.list[i])
      }
    }
    console.log(goods)
    if (goods.length <= 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.setStorageSync('goods', goods);
      if (_this.data.beforeprice == _this.data.pricees) {
        wx.setStorageSync('price', _this.data.pricees);
      } else {
        wx.setStorageSync('price', _this.data.price);
      }
      wx.setStorageSync('fee', _this.data.fee);
      wx.setStorageSync('goodslistlength', _this.data.list.length);
      //发货前
      if(_this.data.before==1){
        wx.redirectTo({
          url: '/pages/sale/refund/refund',
        })
      }
      //发货后
      if(_this.data.before==2){
        wx.redirectTo({
          url: '/pages/sale/sale/sale',
        })
      }
     
    }

  },




})